package wongsi.demo.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;

@Slf4j
public class WaitTest {
    public static void main(String[] args) {

        AtomicBoolean isNotify = new AtomicBoolean(false);
        final Object lock = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                if (!isNotify.get()) {
                    try {
                        lock.wait(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    log.info("wait {}", System.currentTimeMillis());
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                lock.notify();
                log.info("notify {}", System.currentTimeMillis());
                isNotify.set(true);
            }
        });

        t1.start();
        t2.start();
    }
}
